#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    //1.创建匿名管道
    int fd[2] = {0};
    if(pipe(fd) < 0)
    {
        perror("pipe");
        return 1;
    }
    //2.创建父子进程
    pid_t pid = fork();
    if(pid == 0)
    {
        //子进程一直向管道写入信息
        close(fd[0]);//子进程关闭读端
        const char * msg = "hello father,child here\n";
        int count = 10;
        while(count--)
        {
            write(fd[1],msg,strlen(msg));
            sleep(1);
        }
        close(fd[1]);
        exit(0);
    }
    //3.关闭父进程读端，接收子进程状态
    close(fd[1]);//父进程关闭写端
    close(fd[0]);//父进程关闭读端
    int status = 0;
    waitpid(pid,&status,0);
    //4.打印子进程状态中的信号码
    printf("child recv signal:%d\n",status & 0x7f);
    return 0;
}